﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DddCms.Infrastructure.Repository;
using Rhino.Mocks;
using System.Data.Entity;
using DddCms.Domain.Model;
using DddCms.Infrastructure.Authentication;
using DddCms.Domain.Utility;

namespace DddCms.Infrastructure.Test.Repository
{
    /// <summary>
    /// Test the DatabaseInitializer class
    /// </summary>
    [TestClass]
    public class DatabaseInitializerTest
    {
        public int MyProperty { get; set; }
        /// <summary>
        /// Fixture params of object methode
        /// </summary>
        public DataContextMock DataContext { get; set; }

        /// <summary>
        /// Initialize the test
        /// </summary>
        [TestInitialize]
        public void TestInitialize()
        {
            DataContext = new DataContextMock();
            DataContext.User = MockRepository.GenerateStrictMock<DbSetMock>();
        }
        /// <summary>
        /// Test si les tables sont bien rempli apres l'initialisation
        /// </summary>
        [TestMethod]
        public void DatabaseInitializerSeedTest()
        {
            // Init
            PasswordUtil PasswordUtil = new PasswordUtil();
            DataContext.PasswordUtil = PasswordUtil;
            DataContext
                .User
                .Expect(user => user
                    .Add(Arg<User>.Matches(u => u.UserName == "admin"
                        && u.Password == PasswordUtil.encryption("admin"))));
           
            DatabaseInitializerMock dataInitializer = new DatabaseInitializerMock();
            dataInitializer.LoggingUtil = MockRepository.GenerateStrictMock<ILoggingUtil>();
            dataInitializer.LoggingUtil.Expect(l => l.Information("Database", "Initializer", "Initialisation de la base de donné."));

            //Run
            dataInitializer.fakeSeed(DataContext);
            
            // Verify
            Assert.AreEqual(DataContext.SaveCalled, true);
            DataContext
                .User.VerifyAllExpectations();
        }
    }
}
